package com.ymatou.query.seller.trading.api.testcase;

import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.query.seller.trading.api.parameter.Common;
import com.ymatou.query.seller.trading.api.parameter.GetSellerHistoryOrdersListArguments;
import com.ymatou.query.seller.trading.api.parameter.GetSellerHistoryOrdersListBean;
import com.ymatou.query.seller.trading.api.parameter.GetSellerHistoryOrdersListResponse;
import com.ymatou.query.seller.trading.api.parameter.SellerHistoryOrder;
import com.ymatou.query.seller.trading.api.parameter.Sp_GetSellerHistoryOrdersList;
import com.ymatou.query.seller.trading.api.service.GetSellerHistoryOrdersListCall;
import com.ymatou.query.seller.trading.api.service.OrderCreate;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.common.enums.OrderStatusEnum;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 获取卖家历史订单列表 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetSellerHistoryOrdersList {

	private static GetSellerHistoryOrdersListBean getsellerhistoryorderslistbean;
	private static GetSellerHistoryOrdersListCall getsellerhistoryorderslistcall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取卖家历史订单列表");
	}

	@Before
	public void caseUp() {
		getsellerhistoryorderslistbean = new GetSellerHistoryOrdersListBean();
		getsellerhistoryorderslistcall = new GetSellerHistoryOrdersListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getsellerhistoryorderslistcall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {
		GetSellerHistoryOrdersListResponse expectResponse = ConstructExpectedResponse();
		GetSellerHistoryOrdersListResponse actualResponse = getsellerhistoryorderslistcall
				.getSellerHistoryOrdersListResponse();
		DataUtil.verifyEquals(expectResponse.getOrderList().size(),
				actualResponse.getOrderList().size(), "验证orderList的大小");
		for (SellerHistoryOrder expOrder : expectResponse.getOrderList()) {
			String expPid = expOrder.getProductId();
			boolean exists = false;
			if (null == expPid) {
				exists = actualResponse.getOrderList().stream()
						.anyMatch(x -> x.getProductId() == null);
			} else {
				exists = actualResponse.getOrderList().stream()
						.anyMatch(x -> x.getProductId().equals(expPid));
			}
			DataUtil.verifyEquals(true, exists, "验证期望的productId:" + expPid
					+ "在接口返回列表中");
			if (exists) {
				SellerHistoryOrder actualOrder = actualResponse.getOrderList()
						.stream().filter(x -> x.getProductId().equals(expPid))
						.collect(Collectors.toList()).get(0);
				DataUtil.verifyDateEquals(expOrder.getLastOrderTime(),
						actualOrder.getLastOrderTime(), "验证productId:" + expPid
								+ "的lastOrderTime");
				DataUtil.verifyEquals(expOrder.getNotPaidNum(),
						actualOrder.getNotPaidNum(), "验证productId:" + expPid
								+ "的notPaidNum");
				DataUtil.verifyEquals(expOrder.getSaleNum(),
						actualOrder.getSaleNum(), "验证productId:" + expPid
								+ "的saleNum");
			}
		}
	}

	private GetSellerHistoryOrdersListResponse ConstructExpectedResponse() {
		GetSellerHistoryOrdersListArguments args = new GetSellerHistoryOrdersListArguments();
		args.setSellerId(getsellerhistoryorderslistbean.getSellerId());
		args.setActivityId(getsellerhistoryorderslistbean.getActivityId());
		args.setLatestTime(YMTDateUtil.addYears(1, YMTDateUtil.YMDHMS));
		args.setOrderEstablishStatus(OrderStatusEnum.OrderEstablish.getValue());
		args.setLastProductId(getsellerhistoryorderslistbean.getLastProductId());
		args.setTopCount(getsellerhistoryorderslistbean.getOrderNum());

		args.setOrderStatus(Common.OrderAvailiableStatus());
		return Execute(args);
	}

	private GetSellerHistoryOrdersListResponse Execute(
			GetSellerHistoryOrdersListArguments args) {
		Sp_GetSellerHistoryOrdersList sp = new Sp_GetSellerHistoryOrdersList();
		sp.setLastProductId(args.getLastProductId());
		sp.setSellerId(args.getSellerId());
		sp.setActivityId(args.getActivityId());
		sp.setTime(args.getLatestTime());
		sp.setOrderEstablishStatus(args.getOrderEstablishStatus());
		sp.setTop(args.getTopCount());
		sp.setOrderStatus(args.getOrderStatus());
		return sp.Execute();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerHistoryOrdersList_001() {
		Logger.start(true, "获取卖家历史订单列表成功");
		try {
			Logger.comment("创建订单");
			PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
			int userId =placeOrderResp.getUserId();         
			int tradingId = placeOrderResp.getTradingId();

		
			String lastProductId ="111";
			Logger.comment("创建订单结束");
			// 参数
			getsellerhistoryorderslistbean.setActivityId(11);
			getsellerhistoryorderslistbean.setLastProductId(lastProductId);
			getsellerhistoryorderslistbean.setOrderNum(10);
			getsellerhistoryorderslistbean.setSellerId(sellerId);
			// 发送请求
			getsellerhistoryorderslistcall
					.setData(getsellerhistoryorderslistbean);
			getsellerhistoryorderslistcall.callService();
			Logger.comment("getsellerhistoryorderslistcall send:"
					+ getsellerhistoryorderslistcall.getEntityString());
			Logger.comment("getsellerhistoryorderslistcall response:"
					+ getsellerhistoryorderslistcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerhistoryorderslistcall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerHistoryOrdersList_002() {
		Logger.start(true, "获取卖家历史订单列表成功");
		try {
			Logger.comment("创建订单");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate create = new OrderCreate();
			create.create1_M2cOrder(userId, sellerId);
			String lastProductId = create.create2_PayOrder(userId, sellerId)
					.getsProductId();
			Logger.comment("创建订单结束");
			// 参数
			getsellerhistoryorderslistbean.setActivityId(46870);
			getsellerhistoryorderslistbean.setLastProductId(lastProductId);
			getsellerhistoryorderslistbean.setOrderNum(10);
			getsellerhistoryorderslistbean.setSellerId(sellerId);
			// 发送请求
			getsellerhistoryorderslistcall
					.setData(getsellerhistoryorderslistbean);
			getsellerhistoryorderslistcall.callService();
			Logger.comment("getsellerhistoryorderslistcall send:"
					+ getsellerhistoryorderslistcall.getEntityString());
			Logger.comment("getsellerhistoryorderslistcall response:"
					+ getsellerhistoryorderslistcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerhistoryorderslistcall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerHistoryOrdersList_003() {
		Logger.start(true, "获取卖家历史订单列表成功, 不传lastProductId");
		try {
			Logger.comment("创建订单");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate create = new OrderCreate();
			create.create1_M2cOrder(userId, sellerId);
			Logger.comment("创建订单结束");
			// 参数
			getsellerhistoryorderslistbean.setActivityId(11);
			getsellerhistoryorderslistbean.setOrderNum(10);
			getsellerhistoryorderslistbean.setSellerId(sellerId);
			// 发送请求
			getsellerhistoryorderslistcall
					.setData(getsellerhistoryorderslistbean);
			getsellerhistoryorderslistcall.callService();
			Logger.comment("getsellerhistoryorderslistcall send:"
					+ getsellerhistoryorderslistcall.getEntityString());
			Logger.comment("getsellerhistoryorderslistcall response:"
					+ getsellerhistoryorderslistcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerhistoryorderslistcall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetSellerHistoryOrdersList_004() {
		Logger.start(true, "获取卖家历史订单列表失败，没有传sellerId");
		try {
			Logger.comment("创建订单");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate create = new OrderCreate();
			create.create1_M2cOrder(userId, sellerId);
			String lastProductId = create.create2_PayOrder(userId, sellerId)
					.getsProductId();
			Logger.comment("创建订单结束");
			// 参数
			getsellerhistoryorderslistbean.setActivityId(11);
			getsellerhistoryorderslistbean.setLastProductId(lastProductId);
			getsellerhistoryorderslistbean.setOrderNum(10);
			// getsellerhistoryorderslistbean.setSellerId(sellerId);
			// 发送请求
			getsellerhistoryorderslistcall
					.setData(getsellerhistoryorderslistbean);
			getsellerhistoryorderslistcall.callService();
			Logger.comment("getsellerhistoryorderslistcall send:"
					+ getsellerhistoryorderslistcall.getEntityString());
			Logger.comment("getsellerhistoryorderslistcall response:"
					+ getsellerhistoryorderslistcall.getReturnData());
			Logger.verifyEquals(ECode.FAILED400,
					getsellerhistoryorderslistcall.getCode(), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetSellerHistoryOrdersList_005() {
		Logger.start(true, "获取卖家历史订单列表失败, 没有传orderNum");
		try {
			Logger.comment("创建订单");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate create = new OrderCreate();
			create.create1_M2cOrder(userId, sellerId);
			String lastProductId = create.create2_PayOrder(userId, sellerId)
					.getsProductId();
			Logger.comment("创建订单结束");
			// 参数
			getsellerhistoryorderslistbean.setActivityId(11);
			getsellerhistoryorderslistbean.setLastProductId(lastProductId);
			// getsellerhistoryorderslistbean.setOrderNum(10);
			getsellerhistoryorderslistbean.setSellerId(sellerId);
			// 发送请求
			getsellerhistoryorderslistcall
					.setData(getsellerhistoryorderslistbean);
			getsellerhistoryorderslistcall.callService();
			Logger.comment("getsellerhistoryorderslistcall send:"
					+ getsellerhistoryorderslistcall.getEntityString());
			Logger.comment("getsellerhistoryorderslistcall response:"
					+ getsellerhistoryorderslistcall.getReturnData());
			Logger.verifyEquals(ECode.FAILED400,
					getsellerhistoryorderslistcall.getCode(), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetSellerHistoryOrdersList_006() {
		Logger.start(true, "获取卖家历史订单列表失败, 没有传ActivityId");
		try {
			Logger.comment("创建订单");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate create = new OrderCreate();
			create.create1_M2cOrder(userId, sellerId);
			String lastProductId = create.create2_PayOrder(userId, sellerId)
					.getsProductId();
			Logger.comment("创建订单结束");
			// 参数
			// getsellerhistoryorderslistbean.setActivityId(11);
			getsellerhistoryorderslistbean.setLastProductId(lastProductId);
			getsellerhistoryorderslistbean.setOrderNum(10);
			getsellerhistoryorderslistbean.setSellerId(sellerId);
			// 发送请求
			getsellerhistoryorderslistcall
					.setData(getsellerhistoryorderslistbean);
			getsellerhistoryorderslistcall.callService();
			Logger.comment("getsellerhistoryorderslistcall send:"
					+ getsellerhistoryorderslistcall.getEntityString());
			Logger.comment("getsellerhistoryorderslistcall response:"
					+ getsellerhistoryorderslistcall.getReturnData());
			Logger.verifyEquals(ECode.FAILED400,
					getsellerhistoryorderslistcall.getCode(), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}